/**
 * --------------------------------------------------------------------------
 * Hi.js (v1.0.0): jquery.collapse.js
 * require jquery
 * --------------------------------------------------------------------------
 */
;
(function($, window, document, undefined) {
    'use strict';

    function init(target) {
        var opts = $.data(target, 'collapse');
        var options = opts.options;
        var $element = $(target);

        opts.panels = [];
        $element.children('.collapse-item').each(function() {
            var poptions = $.extend({}, $.parser.parseOptions(this), {
                selected: $(this).hasClass('collapse')
            });

            var pp = $(this);
            $.data(this, 'collapse-item', poptions);
            opts.panels.push(pp);

            var head = $(pp).find('.collapse-head');
            head.bind('click', function() {
                togglePanel(target, pp);
                return false;
            });
        });

        function togglePanel(target, panel) {
            if (panel.hasClass('disabled')) {
                return;
            }
            var opts = $.data(target, 'collapse');
            var options = opts.options;
            var panels = opts.panels;;
            var index = getPanelIndex(target, panel);
            var $elem = $(target);
            var $body = panel.children('.collapse-body');

            switch (options.animate) {
                case 'slide':
                    $elem.hasClass('collapse') ?
                        $body.slideUp(options.duration) :
                        $body.slideDown(options.duration);
                    break;
                case 'fade':
                    $body.fadeIn(options.duration);
                    break;
                default:
                    $body.show(options.duration);
                    break;
            }
            if ($elem.hasClass('collapse')) {
                panels[index].removeClass('collapse');
            } else {
                panels[index].addClass('collapse');
            }
            if (options.multiple) {
                for (var i = 0; i < panels.length; i++) {
                    panels[i].removeClass('collapse');
                }
                panels[index].addClass('collapse');
            }
        }
    }

    function setSize(target, param) {
        var opts = $.data(target, 'collapse');
        var options = opts.options;
        var panels = opts.panels;
        var $elem = $(target);
        var isHorizontal = (options.halign == 'left' || options.halign == 'right');

        if (param) {
            $.extend(opts, {
                width: param.width,
                height: param.height
            });
        }

        $elem.size(opts);
    }

    function getSelections(target) {}

    function getSelected(target) {}

    function getPanel(target, which) {
        var panels = $.data(target, 'collapse').panels;
        if (typeof which == 'number') {
            if (which < 0 || which >= panels.length) {
                return null;
            } else {
                return panels[which];
            }
        }
        return findBy(target, 'data-title', which);
    }

    function getPanelIndex(target, panel) {
        return findBy(target, null, panel);
    }

    function findBy(target, property, value, all) {
        var panels = $.data(target, 'collapse').panels;
        var pp = [];
        for (var i = 0; i < panels.length; i++) {
            var p = panels[i];
            if (property) {
                if ($(p).attr(property) == value) {
                    pp.push(p);
                }
            } else {
                if (p[0] == $(value)[0]) {
                    return i;
                }
            }
        }
        if (property) {
            return all ? pp : (pp.length ? pp[0] : null);
        }
        return -1;
    }

    function stopAnimate(target) {
        var panels = $.data(target, 'collapse').panels;
        for (var i = 0; i < panels.length; i++) {
            panels[i].stop(true, true);
        }
    }

    function select(target, which) {
        var p = getPanel(target, which);
        if (!p) {
            return;
        }
        stopAnimate(target);
        var options = $.data(target, 'collapse').options;
    }

    function unselect(target, which) {
        var p = getPanel(target, which);
        if (!p) {
            return;
        }
        stopAnimate(target);
        var options = $.data(target, 'collapse').options;
    }

    function add(target) {}

    function remove(target) {}

    $.fn.collapse = function(options, param) {
        if (typeof options == 'string') {
            return $.fn.collapse.methods[options](this, param);
        }
        options = options || {};
        return this.each(function() {
            var state = $.data(this, 'collapse');
            if (state) {
                $.extend(state.options, options);
            } else {
                $.data(this, 'collapse', {
                    options: $.extend({}, $.fn.collapse.defaults, $.fn.collapse.parseOptions(this), options),
                    collapse: $(this).addClass('collapse'),
                    panels: []
                })
                init(this);
            }
            setSize(this);
        });
    };

    $.fn.collapse.VERSION = '1.0.0';

    $.fn.collapse.methods = {
        options: function(jq) {
            return $.data(jq[0], 'collapse').options;
        },
        panels: function(jq) {
            return $.data(jq[0], 'collapse').panels;
        },
        resize: function(jq, param) {
            return jq.each(function() {
                setSize(this, param);
            });
        },
        getSelections: function(jq) {
            return getSelections(jq[0]);
        },
        getSelected: function(jq) {
            return getSelected(jq[0]);
        },
        getPanel: function(jq, which) {
            return getPanel(jq[0], which);
        },
        getPanelIndex: function(jq, panel) {
            return getPanelIndex(jq[0], panel);
        },
        select: function(jq, which) {
            return jq.each(function() {
                select(this, which);
            });
        },
        unselect: function(jq, which) {
            return jq.each(function() {
                unselect(this, which);
            });
        },
        add: function(jq, options) {
            return jq.each(function() {
                add(this, options);
            });
        },
        remove: function(jq, param) {
            return jq.each(function() {
                remove(this, param);
            });
        }
    };
    $.fn.collapse.parseOptions = function(target) {
        var $element = $(target);
        var options = $.extend({}, $.parser.parseOptions($element, ['width', 'height', 'halign'], {
            fit: 'boolean',
            border: 'boolean',
            animate: 'string',
            multiple: 'boolean',
            selected: 'number'
        }));
        return options;
    }

    $.fn.collapse.defaults = {
        width: 'auto',
        height: 'auto',
        fit: false,
        border: true,
        animate: 'slide',
        duration: 400,
        multiple: false,
        selected: 0,
        halign: 'top',

        onSelect: $.noop,
        onUnselect: $.noop,
        onAdd: $.noop,
        onBeforeRemove: $.noop,
        onRemove: $.noop
    };

    setTimeout(function() {
        $('[data-toggle=collapse]').collapse();
    }, 0);

})(jQuery, window, document);